package com.ruoyi.iot.service.impl;

import com.ruoyi.iot.domain.IotSensorMsg;
import com.ruoyi.iot.mapper.IotSensorMsgMapper;
import com.ruoyi.iot.service.IIOTSensorMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IOTSensorMsgServiceImpl implements IIOTSensorMsgService {

    @Resource
    private IotSensorMsgMapper iotSensorMsgMapper;

    /**
 * 根据远程ID和报告时间检查物联网传感器消息是否存在
 *
 * @param remoteId 远程ID，用于标识传感器的唯一性
 * @param reportTime 报告时间，用于区分不同的传感器消息
 * @return 如果存在对应的物联网传感器消息则返回true，否则返回false
 */
@Override
public boolean existsByRemoteIdAndReportTime(String remoteId, String reportTime) {
    // 检查输入参数是否为null，如果任一参数为null，则直接返回false
    if (remoteId == null || reportTime == null) {
        return false;
    }
    // 使用MyBatis-Plus的QueryWrapper构建查询条件，查询满足条件的物联网传感器消息
//    List<IotSensorMsg> sensorMsgs = iotSensorMsgMapper.selectList(new LambdaQueryWrapper<IotSensorMsg>().eq(IotSensorMsg::getRemoteId, remoteId).eq(IotSensorMsg::getReportTime, reportTime));
    List<IotSensorMsg> sensorMsgs = iotSensorMsgMapper.selectOneByRemoteIdAndReportTime(remoteId,reportTime);
    // 如果查询结果不为null，则说明存在满足条件的物联网传感器消息，返回false
    if (sensorMsgs != null){
        return false;
    }
    // 如果查询结果为null，说明不存在满足条件的物联网传感器消息，返回true
    return true;
}


    @Override
    public void batchInsert(List<IotSensorMsg> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        iotSensorMsgMapper.batchInsert(list);
    }

    @Override
    public List<IotSensorMsg> selectSensorMsgByType(String type) {
        return iotSensorMsgMapper.selectSensorMsgByType( type);
    }

    @Override
    public int batchUpdateState(List<IotSensorMsg> msgList) {
        return iotSensorMsgMapper.batchUpdateState(msgList);
    }
}
